/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if !defined(___GRAPH2D_BUTTON_H___)
#define ___GRAPH2D_BUTTON_H___

#include "imageButton.h"
#include "text.h"
#include <cstdarg>
#include <string>

namespace Graph2D
{
	class Button : public ImageButton
	{
		DISALLOW_COPY_AND_ASSIGN(Button);

		typedef ImageButton super;

	public:
		Button();
		explicit Button(const char* text);
		explicit Button(const std::string& text);
		virtual ~Button();

		virtual const char* getTypeName() const
		{
			return "Button";
		}

		void setSize(const Vector2& size);
		void setSize(const float w, const float h)
		{
			setSize(Vector2(w, h));
		}

		void adjustSize();

		const std::string& getString() const;
		void setString(const std::string& text);
		void format(const char* fmt, ...);

		void setFontSize(const float fontSize);

		//! 水平方向のテキストアライメントを取得
		TextAlignmentH getTextAlignmentH() const;
		//! 水平方向のテキストアライメントを設定
		void setTextAlignmentH(const TextAlignmentH alignment);

		//! 垂直方向のテキストアライメントを取得
		TextAlignmentV getTextAlignmentV() const;
		//! 垂直方向のテキストアライメントを設定
		void setTextAlignmentV(const TextAlignmentV alignment);

		void setTextColor(const Color& color);
		void setTextColor(const float r, const float g, const float b, const float a)
		{
			setTextColor(Color(r, g, b, a));
		}

		const std::string& getTagString() const;
		void setTagString(const std::string& text);
		void formatTag(const char* fmt, ...);
		void replace(const char* key, const char* word);

		virtual void onSerialize(mana_stream* stream) const;
		virtual void onDeserialize(mana_stream* stream);
		virtual void onUpdate(const UpdateInfomation& updateInfomation);
		virtual bool onDraw(const DrawRect& drawRect);
		virtual bool onTouchesBegan(const Vector2& localPosition);
		virtual bool onTouchesEnded(const Vector2& localPosition);
		virtual bool onTouchesCancelled(const Vector2& localPosition);
		virtual void onSizeChanged();

		virtual bool compare(const Button& other) const;

	private:
		void initialize();

	private:
		Text label;
		Text tag;
		unsigned char textAlignmentH;
		unsigned char textAlignmentV;
		bool dirty:8;
		char padding;
	};
}

#endif
